<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-05-24 14:39:10
 * @Description: 脚本清理指定用户的订单账务
 */
$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
DB::connection()->disableQueryLog(); 


$order_info=DB::select("SELECT * FROM `car_wash_order` where `status`=1417");
//循环处理订单
foreach($order_info as $order){
	DB::transaction(function () use ($order) {
	  	$order = CarWashOrder::where("order_id",$order->order_id)->lockForUpdate()->first();
	    $order_amount=$order->pay_amount;
	    $tran_no = date('Ymd',time()).Common::generateCode();   
	    $user = Users::find($order->user_id);
	    $o_p=CarWashOrderPayRecord::where('order_id',$order->order_id)->where("status",1)->first();   
	    $o_p->tran_no=$tran_no;
	    $o_p->save();
	    $pay_type=$order->pay_type;
	    if($pay_type== 1||$pay_type== 7){//现金 或者银行卡支付  
	        //记录用户充值 不记录充值记录  只记录流水
	        Account::record($order->user_id,$order->shop_id,1,$tran_no,1,106,$order_amount,'洗车单结算充值,洗车单号:'.$order->order_id);
	        //记录用户支出
	        Account::record($order->user_id,$order->shop_id,1,$tran_no,2,206,$order_amount,'洗车单结算支出,洗车单号:'.$order->order_id);
	        //记录公司账户收入
	        Account::record_base('1',$tran_no,1,106,$order_amount,'洗车单结算收入,洗车单号:'.$order->order_id);
	        //记录订单账务关系
	        CarWashOrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));
	    }

	    if($pay_type== 3){//微信支付    
	        Account::record($order->user_id,$order->shop_id,1,$tran_no,1,106,$order_amount,'洗车单结算充值,洗车单号:'.$order->order_id);
	        //记录用户支出
	        Account::record($order->user_id,$order->shop_id,1,$tran_no,2,206,$order_amount,'洗车单结算支出,洗车单号:'.$order->order_id);
	        //记录公司账户收入
	        Account::record_base('1',$tran_no,1,106,$order_amount,'洗车单结算收入,洗车单号:'.$order->order_id);
	        //记录订单账务关系
	        CarWashOrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));
	    }
	    CarWashOrderAccount::where("tran_no",$tran_no)->update(array("created_at"=>$order->pay_time));
	    JournalAccount::where("tran_no",$tran_no)->update(array("created_at"=>$order->pay_time));  

	    

	  /**
	   * @Author:      lyt
	   * @DateTime:  2017-05-25 16:06:52
	   * @Description: 改订单的退款单的逻辑
	   */
	  	$ro=ReturnCarwashOrder::where("order_id",$order->order_id)->where("status",1)->first();//如果存在则
	  	if($ro){
	        $user = Users::find($order->user_id);
	        $ror=CarWashOrderPayRecord::where("order_id",$order->order_id)->first();//找到原洗车单的支付记录
	        $tran_no = date('Ymd',time()).Common::generateCode();
	        $order_amount=$ror->amount;
	        $rwor=ReturnWashOrderRecord::where("return_washorder_id",$ro->id)->first();
	        //各种进行判断
	        if($ror->pay_type== 1 || $ror->pay_type== 7||$ror->pay_type== 3){//现金 或者银行卡支付 或者微信支付

	            Account::record($order->user_id,$order->shop_id,1,$tran_no,1,117,$order_amount,'洗车单退单收入,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
	            //记录用户收入
	            Account::record($order->user_id,$order->shop_id,1,$tran_no,2,217,$order_amount,'洗车单退单支出,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
	            //记录公司账户支出
	            Account::record_base('1',$tran_no,2,217,$order_amount,'洗车单退单支出,洗车单退货单号:'.$ro->id.',洗车单号:'.$ro->order_id);
	            //记录订单账务关系
	            CarWashOrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2,));
	        }   
	        CarWashOrderAccount::where("tran_no",$tran_no)->update(array("created_at"=>$ro->implement_time));
	    	JournalAccount::where("tran_no",$tran_no)->update(array("created_at"=>$ro->implement_time));       
	  	}  

	});
	Log::info(" 订单 ：".$order->order_id." 完毕");
}





